package com.dc.coroutine

import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.util.Log
import android.widget.Button
import androidx.lifecycle.ViewModel
import androidx.lifecycle.ViewModelProvider
import androidx.lifecycle.lifecycleScope
import kotlinx.coroutines.launch

class CoroutineActivity : AppCompatActivity() {

    val TAG = "Coroutine"

    private lateinit var viewModel: FlowViewModel

    private lateinit var btn: Button

    var index = 0

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_coroutine)

        btn = findViewById(R.id.btn)
        viewModel = ViewModelProvider(this).get(FlowViewModel::class.java)
        btn.setOnClickListener {
            viewModel.requestData(index++)
        }

        lifecycleScope.launch {
            viewModel.stateFlow.collect {
                Log.e(TAG, "stateFlow 请求数据：$it")
            }
//            viewModel.sharedFlow.collect {
//                Log.e(TAG, "sharedFlow 请求数据：$it")
//            }
        }

    }
}